#!/usr/bin/perl -w
use 5.010;

if ( ! defined @ARGV) {
    die "usage: $0 <k|g|p> <file> \n";
}

open FILE, $ARGV[1] or die "can not open file $ARGV[0]!\n";
@lines = <FILE>;
close FILE;


if ( $ARGV[0] eq 'k') {
    &do_kernel;
}
elsif ( $ARGV[0] eq 'g') {
    &do_gnu;
}
elsif ( $ARGV[0] eq 'p') {
    &do_python;
}


sub do_kernel {

    my $output1;

    foreach $line (@lines){

	# skip empty comment
	if ($line =~ /^\s*\*\s*$/) {
	    $output1 .= $line;
	    #print $line;
	    next;
	}

	# delete Author
	$line =~ s|(^\s*\*)\s*Authors? *:.*$|$1|;

	# delete Copyright
	$line =~ s|(^\s*\*?)\s*Copyright .*$|$1|;
	$line =~ s|(^\s*\*)\s*\([cC]\) .*$|$1|;

	# delete Written by
	$line =~ s|(^\s*\*)\s*[Ww]ritten [Bb]y.*$|$1|;

	$line =~ s|(^\s*\*)\s*[o]Original [cC]ode.*$|$1|;
	# delete other authors
	#$line =~ s|(^\s*\*) {4,}[a-zA-Z]+.*$|$1|;
	#$line =~ s|(^\s*\*)\t+[a-zA-Z]+.*$|$1|;


	# delete the whole line with only a '*'
	if ($line !~ /^\s*\*\s*$/) {
	    $output1 .= $line;
	    #print $line;
	}

    }


    # delete GPL comment
    $output1 =~ s|(^\s*\*)\s*This program is free software.*by.*the Free Software Foundation.?\s*([a-zA-Z]*\s*version\s*[^.]*\.)?|$1|sm;

    $output1 =~ s|(^\s*\*)\s*This program is free software.*any.*later.*version.?|$1|sm;
    $output1 =~ s|This program is free software.*any.*later.*version.?||sm;
    $output1 =~ s|(^\s*\*)\s*This program is free software.*Free Software.*by reference.?|$1|sm;
    $output1 =~ s|(^\s*\*)\s*This program is distributed.*GNU General Public License.*details.?|$1|sm;
    $output1 =~ s|This program is distributed.*GNU General Public License.*details.?||sm;

    $output1 =~ s|(^\s*\*)\s*You should have received a copy of the GNU.*write.*the Free Software.*USA.?|$1|sm;

    $output1 =~ s|You should have received a copy of the GNU.*write.*the Free Software.*USA.?||sm;

    $output1 =~ s|(^\s*\*)\s*Subject to the GNU.*Public License[^.]*\.|$1|gm;


    $output1 =~ s|(^\s*\*)\s*Use of this code is subject to the.*GNU general public.*gpl.html.?|$1|sm;

    # rights reserved
    $output1 =~ s|(^\s*\*)\s*All rights reserved.|$1|gm;    

    # licensed
    $output1 =~ s|(^\s*\*)\s*This source code is licensed under the GNU.*more details.?|$1|sm;
    # released
    $output1 =~ s|(^\s*\*)\s*.*released under the[^.]*\.|$1|gm;

    $output1 =~ s|(^\s*\*)\s*General Public License for more details.?|$1|gm;

    # author email
    $output1 =~ s|(^\s*\*)\s*.*[(<]?[a-zA-Z0-9_\.]+@[a-zA-Z0-9]+[\.a-zA-Z]+[)>]?.*$|$1|gm;

    # delete ajcent comment lines
    $output1 =~ s|(^\s*\*+\s*\n){2,}|$1|gm;

    # delete blank comment
    $output1 =~ s|^\s*/\*[*\n\s]+\*/\n||gm;

    print $output1;
    
}

sub do_gnu {

    my $output1;

    $output1 = join '', @lines;

    # copyright
    $output1 =~ s|(^\s*/\*\s*.*)Copyright[^\n]+\n|$1|gsm;
  
    # contributed by
    $output1 =~ s|(^\s*/\*\s*.*)Contributed [bB]y[^\n]+\n|$1|gsm;

    # adapted by
    $output1 =~ s|(^\s*/\*\s*.*)Adapted for[^\n]+\n|$1|gsm;

    # based on
    $output1 =~ s|(^\s*/\*\s*.*)Based on[^\n]+\n|$1|gsm;

    # written by
    $output1 =~ s|(^\s*/\*\s*.*)Written by[^\n]+\n|$1|gsm;

    # email
    $output1 =~ s|(^\s*/\*\s*.*\n)\s+[^\n]+[(<]?[a-zA-Z0-9_\.]+@[a-zA-Z0-9-]+[\.a-zA-Z]+[)>]?[^\n]*\n|$1|gsm;

    
    # GPL
    $output1 =~ s|(^\s*/\*\s*.*)You should have received a copy of the GNU.*General.*write.*to.*the Free.*Software.*USA.?|$1|sm;

    $output1 =~ s@(^\s*/\*\s*.*)The GNU (C|MP) Library is free software.*by.*the Free Software Foundation.?\s*([a-zA-Z]*\s*version\s*([0-9]+(\.[0-9])*)*[^.]*\.)?@$1@sm;
    $output1 =~ s@(^\s*/\*\s*.*)The GNU (C|MP) Library is distributed.*GNU.*General.*Public.*License.*details.?@$1@sm;
    $output1 =~ s|(^\s*/\*\s*.*)This file is part of the GNU[^.]+\.|$1|sm;

    # Free Software Fundation 
    $output1 =~ s|(^\s*/\*\s*.*)Free Software Foundation[^\n]+\n|$1|gsm;


    # multiple blank comment
    $output1 =~ s|(?<a>^\s*/\*\s*([^/\n]+\n)*)(?:\s*\n){2,}(?<b>\s*\*/\n)|$+{a}$+{b}|sm;
   
    # print result
    print $output1;
}
